/**
 * Foxit Software Inc.
 * Copyright(C) 2005-2014, all rights reserved.
 * The following code is copyrighted and contains proprietary information
 * and trade secrets of Foxit Software Inc.

 * Description: DocumentLoader
 * Author: shaojie zhuo
 * Update: shaojie zhuo (2014-05-07 16:46)
 */

define(function (require, exports, module) {  
    var PDFDocument = require('./PDFDocument.js');
    var PDFDocProperties = require('./PDFDocProperties.js');    

    /**
     * Create a document loader by document id.
     * @param {String} docId document id.
     * @private
     * @constructor
     */
    var DocumentLoader = function() {
        /**
         * Create a PDFDocument object by data in JSON format.
         * @param docInfoJSONData PDF data in JSON format.
         * @returns {WebPDF.ImageEngine.PDFDocument} return null if create PDFDocument failed, otherwise return a PDFDocument object.
         * @private
         */
        function _createPDFDoc(docInfoJSONData) {
            var pdfDoc = null;
            //try {
                pdfDoc = new PDFDocument(docInfoJSONData);                
           // }
           // catch (ex) {
              //  console.error(ex);
              //  return null;
           // }
            return pdfDoc;
        }

        /**
         * Load document asynchronize
         * @param {WebPDF.ReaderApp} app instance.
         * @param {String} password of PDF document.
         * @param {Boolean | null} isCheckPassword Whether the request is for checking password or parsing document.
         * @param {Function} doneHandler callback for load document successfully.
         * @param {Function} failedHandler callback for load document failed.
         * @private
         * @constructor
         */
        this.asyncLoadDocument = function(readerApp, pdfPassword,isCheckPassword,doneHandler,failedHandler)
        {
            if(!$.isFunction(doneHandler) || !$.isFunction(failedHandler))
            {
                $.error("both 'doneHandler' and 'failedHandler' must be function.");
                return;
            }
            var manifestUrl = readerApp.getOptions().url + "manifest";
            var requestData = {};
            if(pdfPassword){
                requestData.psd = pdfPassword;
            }
            if(isCheckPassword != null){
                requestData.isCheckPsd = isCheckPassword;
            }
            requestData.formMode = readerApp.isFormMode();
            requestData.s3Key = readerApp.getFileID();
            $.ajax({
                url: manifestUrl,
                dataType: 'json',
                contentType:"application/x-www-form-urlencoded",
                data: requestData,
                success: function (response, status, xhr) {
                    if(!response){
                        failedHandler(response);
                        return;
                    }
                    var error = response.error;
                    if (error == 0) {
                       // try {
                            var docInfo = $.parseJSON(response.docinfo);
                            var pdfDoc = _createPDFDoc(docInfo);
                            //readerApp.setDownloadPDFPermission(response.CanDownload);
                            //readerApp.setDocHash(response.docHash);
                            //readerApp.setFileName(response.docName);
                            //readerApp.setDocPermission(response.DocPemission);
                            //readerApp.setTrail(response.trail);
                            /*var sessionId = response.requestSessionId;
                            if(!sessionId){
                            	sessionId = xhr.getResponseHeader("x-auth-token");
                            }
                            readerApp.setSessionId(sessionId);*/
                            doneHandler(pdfDoc, response);
                        //}
                       // catch (ex) {
                         //   console.error(ex);
                         //   doneHandler(null, response);
                       // }
                    } else {
                        failedHandler(response);
                    }
                },
                error: function () {
                    failedHandler(null);
                }
            });
        };
    };
    module.exports = DocumentLoader;
});

